package org.android.brasil.projetos.oktoberfest2011.view;

import java.util.List;

import org.android.brasil.projetos.oktoberfest2011.R;
import org.android.brasil.projetos.oktoberfest2011.negocio.model.Horario;
import org.android.brasil.projetos.oktoberfest2011.persistencia.HorarioDAO;
import org.android.brasil.projetos.oktoberfest2011.utils.AnalyticsUtils;
import org.android.brasil.projetos.oktoberfest2011.utils.ControleDeBackground;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;

public class HorarioView extends ListActivity {
	private ListView lv;
	private List<Horario> hors;
	private AnalyticsUtils mGa;

	private OnClickListener casaListener = new OnClickListener() {

		public void onClick(View v) {
			setResult(RESULT_OK);
			finish();
			mGa.buttonTrackEvent("Casa");
		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		this.mGa = AnalyticsUtils.getInstance(this);

		setContentView(R.layout.horarios_view);
		bindScreenFields();
		populateFields();
		
		this.mGa.trackScreen(getString(R.string.horario)+ " - view");
	}

	private void populateFields() {
		HorarioDAO.open(getApplicationContext());
		hors = HorarioDAO.consultarListTodos();
		HorarioDAO.close();

		lv.setAdapter(new HorarioAdapter(getApplicationContext(), hors));

	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		/*
		 * if (AgendaView.home) { AgendaView.home = false; setResult(RESULT_OK);
		 * finish(); }
		 */
	}

	private void bindScreenFields() {
		lv = getListView();
		ImageView casa = (ImageView) findViewById(R.id.casa);
		casa.setOnClickListener(casaListener);

		RelativeLayout rl = (RelativeLayout) findViewById(R.idHorarios.relativeLayout01);
		ControleDeBackground.SetBarraSuperiorBackGround(getApplicationContext(), rl);

		lv.setOnItemClickListener(new OnItemClickListener() {

			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
				Horario hor = hors.get(arg2);
				Intent i = new Intent(getApplicationContext(), AgendaTabView.class);
				i.putExtra("DIA", hor.getDia());
				AgendaView.dia = hor.getDia();
				startActivityForResult(i, 0);

				mGa.trackEvent("Horario", "Agenda, dia " + hor.getDia());
			}
		});
	}

	protected void onDestroy() {
		super.onDestroy();
	}
}
